﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Project32CHE.Biz.Unionshop.Models;
using Creative85.Framework.Utilities;
using Creative85.Framework.Data;
using Project32CHE.Biz.UserAccount;
using Project32CHE.Biz.Goods.Model;
using Project32CHE.Biz.CustomerCar.Model;
using Project32CHE.Biz.Goods.Extend.Model;
using Project32CHE.Biz.UserAccount.Models;

namespace BizProxy
{
    static class ExcelHelper
    {
        public static string GetString(this ICell cell)
        {
            string result = null;
            if (cell != null) { result = cell.ToString(); }
            return result;
        }
        public static string GetString(this IRow row, int index)
        {
            var cell = row.GetCell(index);
            return cell.GetString();
        }
        public static decimal GetDecimal(this ICell cell)
        {
            var result = decimal.Zero;
            var s = cell.GetString();
            decimal.TryParse(s, out result);
            return result;
        }
        public static decimal GetDecimal(this IRow row, int index)
        {
            var cell = row.GetCell(index);
            return cell.GetDecimal();
        }
        public static decimal? GetDecimalNullable(this ICell cell)
        {
            var result = decimal.Zero;
            var s = cell.GetString();
            if (!decimal.TryParse(s, out result)) { return null; }
            return result;
        }
        public static decimal? GetDecimalNullable(this IRow row, int index)
        {
            var cell = row.GetCell(index);
            return cell.GetDecimalNullable();
        }
        public static DateTime GetDateTime(this ICell cell)
        {
            var result = DateTime.MinValue;
            var s = cell.GetString();
            DateTime.TryParse(s, out result);
            return result;
        }
        public static DateTime GetDateTime(this IRow row, int index)
        {
            var cell = row.GetCell(index);
            return cell.GetDateTime();
        }
        public static int GetInt(this ICell cell)
        {
            var result = 0;
            var s = cell.GetString();
            int.TryParse(s, out result);
            return result;
        }
        public static int GetInt(this IRow row, int index)
        {
            var cell = row.GetCell(index);
            return cell.GetInt();
        }
    }
    public static class ImportService
    {
        public class GoodsModel : Goods
        {
            public decimal? InitInventory { get; set; }
            public decimal? InitCostPrice { get; set; }
            public string StorageName { get; set; }
            public string CategoryName { get; set; }

            public Guid StorageId { get; set; }
            public bool IsError { get; set; }
            public string ErrorMsg { get; set; }
            public string Operate { get; set; }
            public int Index { get; set; }
            public string ErrorResult { get; set; }
        }
        public class CustomerCarModel
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string MobilePhone { get; set; }
            public string PlateNumber { get; set; }

            public string VIN { get; set; }
            public string CarModel { get; set; }
            public string Color { get; set; }
            public string EngineCode { get; set; }
            public decimal? Milage { get; set; }
            public string EngineType { get; set; }
            public int? NextCareMilage { get; set; }
            public DateTime? NextCareDate { get; set; }
            public DateTime? NextReturnDate { get; set; }
            public DateTime? InsuranceExpireDate { get; set; }
            public DateTime? NextAuditDate { get; set; }
            public string BuyDate { get; set; }
            public string InsuranceCompany { get; set; }
            public string InsuranceDescription { get; set; }
            public string CarComment { get; set; }
            public string CustomerTag { get; set; }
            public string WorkUnit { get; set; }
            public string TelePhone { get; set; }
            public string CustomerComment { get; set; }

            public Shop Shop { get; set; }
            public User User { get; set; }
            public DateTime CreateTime { get; set; }
            public bool IsError { get; set; }
            public string ErrorMsg { get; set; }
            public int Index { get; set; }
            public string ErrorResult { get; set; }
            public string Operate { get; set; }
        }
        public class ItemModel : Item
        {
            public bool IsError { get; set; }
            public string ErrorMsg { get; set; }
            public int Index { get; set; }
            public string ErrorResult { get; set; }
            public string Operate { get; set; }
        }
        public class SupplierModel : Supplier
        {
            public bool IsError { get; set; }
            public string ErrorMsg { get; set; }
            public int Index { get; set; }
            public string ErrorResult { get; set; }
            public string Operate { get; set; }
        }

        public static T FromExcel1<T>(T t, Action<List<T>, T> filter) where T : class
        {
            return t;
        }
        public static List<T> FromExcel<T>(Stream stream, bool skipFirstRow, Func<IRow, T> func, Action<List<T>, T> filter) where T : class
        {
            var list = new List<T>();
            T item = default(T);
            //var isOldExcel = Path.GetExtension(filePath).Equals(".xls", StringComparison.OrdinalIgnoreCase);
            IWorkbook workbook = null;
            ISheet sheet = null;
            using (stream)
            {
                //if (isOldExcel)
                //{
                //    workbook = new HSSFWorkbook(stream);
                //}
                //else
                //{
                    workbook = new XSSFWorkbook(stream);
                //}
                sheet = workbook.GetSheetAt(0);

                var startRowNum = sheet.FirstRowNum;
                var endRowNum = sheet.LastRowNum;
                if (skipFirstRow) { startRowNum += 1; }
                IRow row = null; bool isBreak;
                try
                {
                    for (int rowIndex = startRowNum; rowIndex <= endRowNum; rowIndex++)
                    {
                        row = sheet.GetRow(rowIndex);
                        if (row == null) { continue; }
                        isBreak = false;
                        for (int cellIndex = row.FirstCellNum, cellMaxIndex = row.LastCellNum; cellIndex <= cellMaxIndex; cellIndex++)
                        {
                            if (isBreak = !string.IsNullOrWhiteSpace(row.GetCell(cellIndex).GetString()))
                            {
                                break;
                            }
                        }
                        if (isBreak)
                        {
                            item = func(row);
                            if (filter != null)
                            {
                                filter(list, item);
                            }
                            if (item != null) { list.Add(item); }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (row != null) { throw new NotSupportedException(string.Format("在读取第{0}行时发生错误[{1}]！", row.RowNum, ex.Message), ex); }
                    else { throw; }
                }
            }
            sheet = null;
            workbook = null;
            return list;
        }
        public static List<T> FromExcel<T>(Stream stream, bool skipFirstRow, int startRowNum, int maxRowNum, Func<NPOI.SS.UserModel.IRow, T> func, Action<List<T>, T> filter) where T : class
        {
            var list = new List<T>();
            T item = default(T);
            //var isOldExcel = Path.GetExtension(filePath).Equals(".xls", StringComparison.OrdinalIgnoreCase);
            IWorkbook workbook = null;
            ISheet sheet = null;
            using (stream)
            {
                //if (isOldExcel)
                //{
                //    workbook = new HSSFWorkbook(stream);
                //}
                //else
                //{
                workbook = WorkbookFactory.Create(stream);
                //}
                sheet = workbook.GetSheetAt(0);

                //var startRowNum = sheet.FirstRowNum;
                var endRowNum = sheet.LastRowNum;
                if (endRowNum > maxRowNum + 2)
                {
                    throw new NotSupportedException("您的excel中数据大于500条，系统导入每次支持导入500条，请分批次导入。");
                }
                if (skipFirstRow) { startRowNum += 1; }
                NPOI.SS.UserModel.IRow row = null; bool isBreak;
                try
                {
                    for (int rowIndex = startRowNum; rowIndex <= endRowNum; rowIndex++)
                    {
                        row = sheet.GetRow(rowIndex);
                        if (row == null) { continue; }
                        if (row.Cells.Count == 0 || row.FirstCellNum == -1) { continue; } //处理空行导致报错的问题
                        isBreak = false;
                        for (int cellIndex = row.FirstCellNum, cellMaxIndex = row.LastCellNum; cellIndex <= cellMaxIndex; cellIndex++)
                        {
                            if (isBreak = !string.IsNullOrWhiteSpace(row.GetCell(cellIndex).GetString()))
                            {
                                break;
                            }
                        }
                        if (isBreak)
                        {
                            item = func(row);
                            if (filter != null)
                            {
                                try
                                {
                                    filter(list, item);
                                }
                                catch (Exception e)
                                {

                                    throw;
                                }
                            }
                            if (item != null) { list.Add(item); }
                        }
                    }
                    var a = "";
                }
                catch (Exception ex)
                {
                    if (row != null) { throw new NotSupportedException(string.Format("在读取第{0}行时发生错误[{1}]！", row.RowNum, ex.Message), ex); }
                    else { throw; }
                }
            }
            sheet = null;
            workbook = null;
            return list;
        }
        public static List<T> FromExcel<T>(Stream stream, bool skipFirstRow, int startRowNum, int maxRowNum, Func<NPOI.SS.UserModel.IRow, T> func, Action<List<T>, T,int> filter) where T : class
        {
            var list = new List<T>();
            T item = default(T);
            //var isOldExcel = Path.GetExtension(filePath).Equals(".xls", StringComparison.OrdinalIgnoreCase);
            IWorkbook workbook = null;
            ISheet sheet = null;
            using (stream)
            {
                //if (isOldExcel)
                //{
                //    workbook = new HSSFWorkbook(stream);
                //}
                //else
                //{
                workbook = WorkbookFactory.Create(stream);
                //}
                sheet = workbook.GetSheetAt(0);

                //var startRowNum = sheet.FirstRowNum;
                var endRowNum = sheet.LastRowNum;
                if (endRowNum > maxRowNum + 2)
                {
                    throw new NotSupportedException("您的excel中数据大于500条，系统导入每次支持导入500条，请分批次导入。");
                }
                if (skipFirstRow) { startRowNum += 1; }
                NPOI.SS.UserModel.IRow row = null; bool isBreak;
                try
                {
                    for (int rowIndex = startRowNum; rowIndex <= endRowNum; rowIndex++)
                    {
                        row = sheet.GetRow(rowIndex);
                        if (row == null) { continue; }
                        if (row.Cells.Count == 0 || row.FirstCellNum == -1) { continue; } //处理空行导致报错的问题
                        isBreak = false;
                        for (int cellIndex = row.FirstCellNum, cellMaxIndex = row.LastCellNum; cellIndex <= cellMaxIndex; cellIndex++)
                        {
                            if (isBreak = !string.IsNullOrWhiteSpace(row.GetCell(cellIndex).GetString()))
                            {
                                break;
                            }
                        }
                        if (isBreak)
                        {
                            item = func(row);
                            if (filter != null)
                            {
                                try
                                {
                                    filter(list, item, endRowNum-1);
                                }
                                catch (Exception e)
                                {
                                    throw;
                                }
                            }
                            if (item != null) { list.Add(item); }
                        }
                    }
                    var a = "";
                }
                catch (Exception ex)
                {
                    if (row != null) { throw new NotSupportedException(string.Format("在读取第{0}行时发生错误[{1}]！", row.RowNum, ex.Message), ex); }
                    else { throw; }
                }
            }
            sheet = null;
            workbook = null;
            return list;
        }
        public static GoodsModel ConvertGoods(NPOI.SS.UserModel.IRow row)
        {
            if (row == null) { throw new NullReferenceException(string.Format("变量{0}为空！", row.GetVarName<IRow>(x => row))); }
            GoodsModel result = new GoodsModel()
            {
                ID = Guid.NewGuid(),
                GoodsCode = row.GetString(0),
                GoodsName = row.GetString(1),
                Unit = row.GetString(2),
                InitInventory = row.GetDecimalNullable(3),
                StorageName = row.GetString(4) == null ? "" : row.GetString(4),
                InitCostPrice = row.GetDecimalNullable(5),
                SalePrice = row.GetDecimal(6),
                SafeQty = row.GetDecimalNullable(7),
                Brand = row.GetString(8),
                Spec = row.GetString(9),
                MatchPlateNumber = row.GetString(10),
                Origin = row.GetString(11),
                Location = row.GetString(12),
                CategoryName = row.GetString(13),
                MatchCarModel = row.GetString(14),
                OEMCode = row.GetString(15),
                Comment = row.GetString(16),
            };
            return result;
        }
        public static CustomerCarModel ConvertCustomerCar(NPOI.SS.UserModel.IRow row)
        {
            if (row == null) { throw new NullReferenceException(string.Format("变量{0}为空！", row.GetVarName<IRow>(x => row))); }
            int i; DateTime t;
            CustomerCarModel result = new CustomerCarModel()
            {
                //客户名称	联系手机	车牌号	VIN	车型	车身颜色	发动机号	最新里程	发动机型号	下次保养里程	下次保养时间	下次回访时间	下次保险时间	下次年审时间	购车时间	保险公司	保险备注	车辆备注	客户类型	客户单位	联系电话	客户备注
                Id = Guid.NewGuid(),
                Name = row.GetString(0),
                MobilePhone = row.GetString(1),
                PlateNumber = row.GetString(2),
                VIN = row.GetString(3),
                CarModel = row.GetString(4),
                Color = row.GetString(5),
                EngineCode = row.GetString(6),
                Milage = ((i = row.GetInt(7)).Equals(0) ? null : new int?(i)),
                EngineType = row.GetString(8),
                NextCareMilage = ((i = row.GetInt(9)).Equals(0) ? null : new int?(i)),
                NextCareDate = ((t = row.GetDateTime(10)).Equals(DateTime.MinValue) ? null : new DateTime?(t)),
                NextReturnDate = ((t = row.GetDateTime(11)).Equals(DateTime.MinValue) ? null : new DateTime?(t)),
                InsuranceExpireDate = ((t = row.GetDateTime(12)).Equals(DateTime.MinValue) ? null : new DateTime?(t)),
                NextAuditDate = ((t = row.GetDateTime(13)).Equals(DateTime.MinValue) ? null : new DateTime?(t)),
                BuyDate = row.GetString(14),
                InsuranceCompany = row.GetString(15),
                InsuranceDescription = row.GetString(16),
                CarComment = row.GetString(17),
                CustomerTag = row.GetString(18),
                WorkUnit = row.GetString(19),
                TelePhone = row.GetString(20),
                CustomerComment = row.GetString(21)
            };
            return result;
        }
        public static SupplierModel ConvertSupplier(IRow row)
        {
            if (row == null) { throw new NullReferenceException(string.Format("变量{0}为空！", row.GetVarName<IRow>(x => row))); }
            SupplierModel result = new SupplierModel()
            {
                ID = Guid.NewGuid(),
                SupplierName = row.GetString(0),
                Contactor = row.GetString(1),
                MobilePhone = row.GetString(2),
                TelePhone = row.GetString(3),
                QQ = row.GetString(4),
                BizScope = row.GetString(5),
                Address = row.GetString(6),
                Comment = row.GetString(7)
            };
            return result;
        }



        public static SupplierModel ConvertBillExt(IRow row)
        {
            if (row == null) { throw new NullReferenceException(string.Format("变量{0}为空！", row.GetVarName<IRow>(x => row))); }
            SupplierModel result = new SupplierModel()
            {
                ID = Guid.NewGuid(),
                SupplierName = row.GetString(0),
                Contactor = row.GetString(1),
                MobilePhone = row.GetString(2),
                TelePhone = row.GetString(3),
                QQ = row.GetString(4),
                BizScope = row.GetString(5),
                Address = row.GetString(6),
                Comment = row.GetString(7)
            };
            return result;
        }

        public static ItemModel ConvertItem(IRow row)
        {
            if (row == null) { throw new NullReferenceException(string.Format("变量{0}为空！", row.GetVarName<IRow>(x => row))); }
            ItemModel result = new ItemModel()
            {
                ID = Guid.NewGuid(),
                ItemCode = row.GetString(0),
                ItemName = row.GetString(1),
                CategoryName = row.GetString(2),
                UnitPrice = row.GetDecimal(4),
                WorkHours = row.GetDecimal(3)
            };
            return result;
        }

        private static void privateSave<T>(IEnumerable<T> list, Action<T> action, Db db = null) where T : class
        {
            if (action != null)
            {
                foreach (var item in list) { action(item); }
            }
            if (db == null)
            {
                db = Db.GetInstance(false);
            }
            db.BeginTransaction();
            try
            {
                db.InsertBulk(list);
                db.CompleteTransaction();
            }
            catch
            {
                db.AbortTransaction();
                throw;
            }
        }
        public static void Save<T>(IEnumerable<T> list, Action<T> action) where T : class
        {
            privateSave<T>(list, action, null);
        }
        public static void Save(IEnumerable<GoodsModel> list, Action<GoodsModel> action)
        {
            var db = Db.GetInstance(false);
            db.BeginTransaction();
            try
            {
                if (action != null)
                {
                    list.ToList().ForEach(action);
                }
                var goods = list.Select(x => new Goods()
                {
                    ID = x.ID,
                    Brand = x.Brand,
                    CategoryCode = x.CategoryCode,
                    CategoryID = x.CategoryID,
                    Comment = x.Comment,
                    CostPrice = x.InitCostPrice.Value,
                    CreateTime = x.CreateTime,
                    GoodsCode = x.GoodsCode,
                    GoodsName = x.GoodsName,
                    IsDeleted = x.IsDeleted,
                    Location = x.Location,
                    MatchCarModel = x.MatchCarModel,
                    MatchPlateNumber = x.MatchPlateNumber,
                    OEMCode = x.OEMCode,
                    OperatorName = x.OperatorName,
                    Origin = x.Origin,
                    SafeQty = x.SafeQty,
                    SalePrice = x.SalePrice,
                    ShopID = x.ShopID,
                    ShortSpelling = PinyinUtility.GetShortSpelling(x.GoodsName),
                    Spec = x.Spec,
                    Spelling = PinyinUtility.GetSpelling(x.GoodsName),
                    UnionID = x.UnionID,
                    Unit = x.Unit
                }).ToList();
                var storageItems = list.Select(x => new StorageItem()
                {
                    ID = Guid.NewGuid(),
                    CreateTime = x.CreateTime,
                    GoodsCode = x.GoodsCode,
                    GoodsID = x.ID,
                    GoodsName = x.GoodsName,
                    LastTime = x.CreateTime,
                    Qty = x.InitInventory.Value,
                    ShopID = x.ShopID,
                    StorageID = x.StorageId,
                    UnionID = x.UnionID
                }).ToList();
                privateSave(goods, null, db);
                privateSave(storageItems, null, db);
                db.CompleteTransaction();
            }
            catch
            {
                db.AbortTransaction();
                throw;
            }
        }
        public static void Save(IEnumerable<CustomerCarModel> list, Action<CustomerCarModel> action)
        {
            var db = Db.GetInstance(false);
            db.BeginTransaction();
            try
            {
                if (action != null)
                {
                    list.ToList().ForEach(action);
                }
                var customers = list.Select(x => new Customer()
                {
                    ID = x.Id,
                    CustomerName = x.Name,
                    Mobilephone = x.MobilePhone,
                    CustomerTag = x.CustomerTag,
                    WorkUnit = x.WorkUnit,
                    TelePhone = x.TelePhone,
                    Comment = x.CustomerComment,
                    UnionID = x.Shop.UnionID,
                    ShopID = x.Shop.ID,
                    CreateTime = x.CreateTime,
                    IsDeleted = false,
                    ShortSpelling = PinyinUtility.GetShortSpelling(x.Name),
                    Spelling = PinyinUtility.GetSpelling(x.Name)
                }).ToList();
                var cars = list.Select(x => new Car()
                {
                    ID = Guid.NewGuid(),
                    PlateNumber = x.PlateNumber,
                    VIN = x.VIN,
                    CarModel = x.CarModel,
                    Color = x.Color,
                    EngineCode = x.EngineCode,
                    EngineType = x.EngineType,
                    Milage = x.Milage,
                    NextAuditDate = x.NextAuditDate,
                    NextCareDate = x.NextCareDate,
                    NextCareMilage = x.NextCareMilage,
                    NextReturnDate = x.NextReturnDate,
                    InsuranceCompany = x.InsuranceCompany,
                    InsuranceDescription = x.InsuranceDescription,
                    InsuranceExpireDate = x.InsuranceExpireDate,
                    BuyDate = x.BuyDate,
                    Comment = x.CarComment,
                    CreateTime = x.CreateTime,
                    UnionID = x.Shop.UnionID,
                    ShopID = x.Shop.ID,
                    IsDeleted = false,
                    CustomerID = x.Id,
                    Brand = "其他",
                    Series = "其他"
                }).ToList();
                privateSave(customers, null, db);
                privateSave(cars, null, db);
                //Save<Customer>(list, action);
                //Save<Car>(list, action);
                db.CompleteTransaction();
            }
            catch
            {
                db.AbortTransaction();
                throw;
            }
        }
        public static void Save(IEnumerable<ItemModel> list, Action<ItemModel> action)
        {
            var db = Db.GetInstance(false);
            db.BeginTransaction();
            try
            {
                if (action != null)
                {
                    list.ToList().ForEach(action);
                }
                var Items = list.Select(x => new Item()
                {
                    ID = x.ID,
                    ItemCode = x.ItemCode,
                    ItemName = x.ItemName,
                    CategoryName = x.CategoryName,
                    SortID = x.SortID,
                    UnitPrice = x.UnitPrice,
                    WorkHours = x.WorkHours,
                    UnionID = x.UnionID,
                    ShopID = x.ShopID,
                    CreateTime = x.CreateTime,
                    IsDeleted = false,
                    ShortSpelling = PinyinUtility.GetShortSpelling(x.ItemName),
                    Spelling = PinyinUtility.GetSpelling(x.ItemName)
                }).ToList();
                privateSave(Items, null, db);
                db.CompleteTransaction();
            }
            catch
            {
                db.AbortTransaction();
                throw;
            }
        }
        public static void Save(IEnumerable<SupplierModel> list, Action<SupplierModel> action)
        {
            var db = Db.GetInstance(false);
            db.BeginTransaction();
            try
            {
                if (action != null)
                {
                    list.ToList().ForEach(action);
                }
                var Suppliers = list.Select(x => new Supplier()
                {
                    ID = x.ID,
                    Address = x.Address,
                    BizScope = x.BizScope,
                    Comment = x.Comment,
                    Contactor = x.Contactor,
                    District = x.District,
                    Fax = x.Fax,
                    MobilePhone = x.MobilePhone,
                    QQ = x.QQ,
                    CreateTime = x.CreateTime,
                    ShopID = x.ShopID,
                    SupplierName = x.SupplierName,
                    TelePhone = x.TelePhone,
                    UnionID = x.UnionID,
                    ShortSpelling = PinyinUtility.GetShortSpelling(x.SupplierName),
                    Spelling = PinyinUtility.GetSpelling(x.SupplierName)
                }).ToList();
                privateSave(Suppliers, null, db);
                db.CompleteTransaction();
            }
            catch
            {
                db.AbortTransaction();
                throw;
            }
        }


        //第三方维修单
        public class BillExtSheetModel : BillExtSheet
        {
            public bool IsError { get; set; }
            public string ErrorMsg { get; set; }
            public int Index { get; set; }
            public string ErrorResult { get; set; }
            public string Operate { get; set; }
        }

        //第三方维修单
        public static BillExtSheetModel ConvertBillExtSheet(IRow row)
        {
            if (row == null) { throw new NullReferenceException(string.Format("变量{0}为空！", row.GetVarName<IRow>(x => row))); }
            int i; DateTime t;
            BillExtSheetModel result = new BillExtSheetModel()
            {
                ID = Guid.NewGuid(),
                BillDate = ((t = row.GetDateTime(0)).Equals(DateTime.MinValue) ? null : new DateTime?(t)),
                FinishedDate = ((t = row.GetDateTime(1)).Equals(DateTime.MinValue) ? null : new DateTime?(t)),
                CheckoutDate = ((t = row.GetDateTime(2)).Equals(DateTime.MinValue) ? null : new DateTime?(t)),
                BillCode = row.GetString(3),
                PickName = row.GetString(4),
                CheckoutState = row.GetInt(5),
                PlateNumber = row.GetString(6),
                CarModel = row.GetString(7),
                CurrentMileage = ((i = row.GetInt(8)).Equals(0) ? 0 : new int?(i)),
                VIN = row.GetString(9),
                EngineCode = row.GetString(10),
                CustomerName = row.GetString(11),
                MobilePhone = row.GetString(12),
                AmountMoney = row.GetDecimal(13),
                CheckoutMoney = row.GetDecimal(14),
                StayInMoney = row.GetDecimal(15),
                ItemName = row.GetString(16),
                GoodsName = row.GetString(17),
                SaleQty = row.GetDecimal(18),
                SaleMoney = row.GetDecimal(19),
                Worker = row.GetString(20),
                SaleMan = row.GetString(21),
                Comment = row.GetString(22)
            };
            return result;
        }

        //第三方维修单
        public static void Save(IEnumerable<BillExtSheetModel> list, Action<BillExtSheetModel> action)
        {
            var db = Db.GetInstance(false);
            db.BeginTransaction();
            try
            {
                if (action != null)
                {
                    list.ToList().ForEach(action);
                }
                var obj = list.Select(x => new BillExtSheet()
                {
                    ID = x.ID,
                    BillDate = x.BillDate,
                    FinishedDate = x.FinishedDate,
                    CheckoutDate = x.CheckoutDate,
                    BillCode = x.BillCode,
                    PickName = x.PickName,
                    CheckoutState = x.CheckoutState,
                    PlateNumber = x.PlateNumber,
                    CarModel = x.CarModel,
                    CurrentMileage = x.CurrentMileage,
                    VIN = x.VIN,
                    EngineCode = x.EngineCode,
                    CustomerName = x.CustomerName,
                    MobilePhone = x.MobilePhone,
                    AmountMoney = x.AmountMoney,
                    CheckoutMoney = x.CheckoutMoney,
                    StayInMoney = x.StayInMoney,
                    ItemName = x.ItemName,
                    GoodsName = x.GoodsName,
                    SaleQty = x.SaleQty,
                    SaleMoney = x.SaleMoney,
                    Worker = x.Worker,
                    SaleMan = x.SaleMan,
                    Comment = x.Comment,
                    CreateTime = x.CreateTime,
                    ShopID = x.ShopID,
                    UnionID = x.UnionID
                }).ToList();
                privateSave(obj, null, db);
                db.CompleteTransaction();
            }
            catch
            {
                db.AbortTransaction();
                throw;
            }
        }

    }
}
